﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public static class ExtensionString
    {
        /// <summary>
        /// Convert string to int.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int me_ConvertToInt(this string str)
        {
            if (!String.IsNullOrEmpty(str))
            {
                try
                {
                    return Int32.Parse(str.Trim());
                }
                catch (Exception) { }
            }

            return 0;
        }

        /// <summary>
        /// Checks if the string is not null if not returns the value in the string or returns empty string otherwise
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string me_GetValue(this string str)
        {
            if (!String.IsNullOrEmpty(str))
            {
                return str;
            }
            return String.Empty;
        }

        /// <summary>
        /// Joins 2 strings. if the first one has content it adds a separation betwwen the 2 strings
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <param name="separation">string that will separate the 2 strings</param>
        /// <returns>returns the resulting string from the join</returns>
        public static string me_JoinString(this string str1, string str2, string separation)
        {
            string nameContainer = str1.me_GetValue();
            nameContainer += (!String.IsNullOrEmpty(str1) ? separation.me_GetValue() : String.Empty) + str2.me_GetValue();
            return nameContainer;
        }

        /// <summary>
        /// Checks if the string is valid(not null and not empty) and if its not equal to a specified value
        /// </summary>
        /// <param name="str"></param>
        /// <param name="value">value to compare against</param>
        /// <returns>returns true if is valid and different and false otherwise</returns>
        public static bool me_IsValidAndDifferent(this string str, string value)
        {
            if (!String.IsNullOrEmpty(str) && str != value)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Get the first part of a string.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="value">value that will set where to split the string</param>
        /// <returns></returns>
        public static string me_SplitReturnFirst(this string str, string value)
        {
            if (!String.IsNullOrEmpty(str) && !String.IsNullOrEmpty(value))
            {
                try
                {
                    string[] array = str.Split(new string[] { value }, StringSplitOptions.RemoveEmptyEntries);
                    return array[0];
                }
                catch (Exception) { }
            }
            return str;
        }

        /// <summary>
        /// Get the first part of a string delimited by the max size. if the string is shorter then th max size the string will not be rmezed.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="max">maximum size of the string to return</param>
        /// <returns>returns the string resized to the max size</returns>
        public static string me_TrimToMaxSize(this string str, int max)
        {
            int tempMax = (max < 0) ? 0 : max;
            return (!String.IsNullOrEmpty(str) && (str.Length > tempMax)) ? str.Substring(0, tempMax) : str;
        }

        /// <summary>
        /// Get the first part of a string delimited by the max size and add some text to the end. 
        /// if the string is shorter then th max size the string will not be resized.
        /// Also the endText length will be taken into consideration for the maxsize
        /// </summary>
        /// <param name="str"></param>
        /// <param name="max">maximum size of the string to return</param>
        /// <param name="endText">text to be added to the end of the string</param>
        /// <returns>returns the string resized to the max size</returns>
        public static string me_TrimToMaxSize(this string str, int max, string endText)
        {
            string tempEndText = endText.me_GetValue();
            int tempMax = (max < 0) ? 0 : max;
            int newMaxSize = (tempMax - tempEndText.Length);

            return (!String.IsNullOrEmpty(str) && (str.Length > newMaxSize)) ? str.Substring(0, newMaxSize) + tempEndText : str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="maxsize"></param>
        /// <param name="endText"></param>
        /// <returns></returns>
        public static string me_RemoveHtmlTags(this string text, int maxsize, string endText)
        {
            return text.me_RemoveHtmlTags().me_TrimToMaxSize(maxsize, endText);
        }

        /// <summary>
        /// Tries to remove the html tags from the text
        /// </summary>
        /// <param name="text"></param>
        /// <returns>returns the changed text is successfull or the original text</returns>
        public static string me_RemoveHtmlTags(this string text)
        {
            if (!String.IsNullOrEmpty(text))
            {
                try
                {
                    return System.Text.RegularExpressions.Regex.Replace(text, "<[^>]*(>|$)", String.Empty).Trim();
                }
                catch (Exception) { }
            }
            return text;
        }

        /// <summary>
        /// Tries to remove the html tags from the text
        /// </summary>
        /// <param name="text"></param>
        /// <returns>returns the changed text is successfull or the original text</returns>
        public static string me_SeparateWordsBy(this string text, string separator)
        {
            if (!String.IsNullOrEmpty(text))
            {
                try
                {
                    return System.Text.RegularExpressions.Regex.Replace(text, @"[^\w-]+", separator).Trim();
                }
                catch (Exception) { }
            }
            return text;
        }

        /// <summary>
        /// try to convert the string to a valid guid.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="id"></param>
        /// <returns>returns true if guid is vali or false if was't possible to convert or if guid is empty</returns>
        public static bool me_GetGuidFromString(this string text, out Guid id)
        {
            if (!String.IsNullOrEmpty(text) && Guid.TryParse(text, out id) && id.IsValid())
            {
                return true;
            }

            id = Guid.Empty;
            return false;
        }

        public static Guid me_GetGuid(this string id)
        {
            Guid result = Guid.Empty;

            //try to get pageId from url
            try
            {
                id = (String.IsNullOrEmpty(id) ? String.Empty : id.Trim());
                if (!string.IsNullOrEmpty(id))
                {
                    result = new Guid(id);
                }
            }
            //do nothing => wrong formats
            catch (FormatException) { }
            catch (OverflowException) { }

            return result;
        }

        public static System.Collections.Specialized.NameValueCollection me_RemoveQueryStringParameter(this System.Collections.Specialized.NameValueCollection queryString, string partToRemove)
        {
            if (!String.IsNullOrEmpty(partToRemove))
            {
                try
                {
                    var qs = System.Web.HttpUtility.ParseQueryString(queryString.ToString());
                    qs.Remove(partToRemove);

                    return qs;
                }
                catch (Exception) { }
            }

            return queryString;
        }

        public static System.Collections.Specialized.NameValueCollection me_AddQueryStringParameter(this System.Collections.Specialized.NameValueCollection queryString, string queryParam, string queryValue)
        {
            if (!String.IsNullOrEmpty(queryParam) && !String.IsNullOrEmpty(queryValue))
            {
                try
                {
                    var qs = System.Web.HttpUtility.ParseQueryString(queryString.ToString());

                    qs.Set(queryParam, queryValue);

                    return qs;
                }
                catch (Exception) { }
            }

            return queryString;
        }

    }
}
